﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace GolfSigma.Inventory.Model
{
    [XmlRoot("TeeTimesMetadata")]
    public class TeeTimesMetadata
    {
        [XmlElement("MinPrice")]
        public float MinPrice { get; set; }

        [XmlElement("MaxPrice")]
        public float MaxPrice { get; set; }

        [XmlElement("FirstTeeTime")]
        public DateTime FirstTeeTime { get; set; }

        [XmlElement("LastTeeTime")]
        public DateTime LastTeeTime { get; set; }

        [XmlElement("TotalTeeTimes")]
        public int TotalTeeTimes { get; set; }

        [XmlArray("WebSites")]
        public List<WebSiteMetadata> WebSitesMetadata { get; set; }

        [XmlElement("FilterData")]
        public FilterData FilterData { get; set; }

        public TeeTimesMetadata()
        {
            // Setup our default values
            MinPrice = float.MinValue;
            MaxPrice = float.MinValue;
            FirstTeeTime = DateTime.MinValue;
            LastTeeTime = DateTime.MaxValue;
            TotalTeeTimes = 0;
            WebSitesMetadata = new List<WebSiteMetadata>();
            FilterData = new FilterData();
        }
    }

    public class FilterData : Dictionary<string, string>, IXmlSerializable
    {
        public FilterData() : base() { }

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            // Read our parent element
            reader.Read();

            while (reader.IsStartElement("Item"))
            {
                string key = reader.GetAttribute("key");
                string value = reader.GetAttribute("value");

                // Make sure we have a key
                if (string.IsNullOrEmpty(key))
                    throw new FormatException();

                // Make sure we have a value
                if (string.IsNullOrEmpty(value))
                    throw new FormatException();

                // Add to the dictionary
                this.Add(key, value);

                // Done reading
                reader.Read();
            }

            // Read the end of the element
            reader.Read();
        }

        public void WriteXml(System.Xml.XmlWriter writer) { }

        #endregion
    }
}
